import { http } from './net.js'

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
// code: -1，0 直接执行逻辑
// 1,验证码弹框方式 带 data:order_num
// 2,url方式 带 data:url
// 3,html方式 带 data:url,path:path
function authChannel ({ channelId, cardInfo }){
  return new Promise((resolve, reject) => {
    http({
      url: '/api/index/index?method=detection_channel_authentication',
      data: {
        "bc_id": cardInfo.bc_id,
        "channel_id": channelId
        // "channel_id": 10
      },
      isShowLoading: true,
      successCb: (res) => {
        if (res.data.authentication == 1) {
          return resolve({ code: 0 })
        } else {
          authChannelStep1(res.data.channel_id, cardInfo).then(res => {
            console.log(res)
            return resolve(res)
          })
        }
      }
    })
  })
}

function authChannelStep1(channelId, cardInfo) {
  return new Promise((resolve, reject) => {
    http({
      url: "/api/index/index?method=channel_authentication",
      data: {
        bc_id: cardInfo.bc_id,
        channel_id: channelId
        // channel_id: 10
      },
      successCb: (res) => {
        // TODO
        if (res.data.is_need_SMS == 1) {
          return resolve({ code: 1, data: res.data.order_num })
        } else if (res.data.is_need_SMS == 2) {
          return resolve({ code: 2,data:res.data.url })
          // wx.navigateTo({
          //   url: "/pages/web/index",
          //   success: (res1) => {
          //     res1.eventChannel.emit('acceptDataFromOpenerPage', {
          //       url:res.data.url,
          //       callback:callback
          //     })
          //   },
          // })
        } else if (res.data.is_need_SMS == 3) {
          return resolve({ code: 3, data: res.data.url, path: res.data.path })
          // wx.navigateTo({
          //   url: "/pages/web/index",
          //   success: (res1) => {
          //     res1.eventChannel.emit('acceptDataFromOpenerPage', {
          //       url: res.data.url,
          //       callback: () => {
          //         callback()
          //         http({
          //           url:"/api/index/index?method=del_html",
          //           data: {
          //             path: res.data.path
          //           },
          //           successCb: (res2) => {},
          //           failCb: (err2) => {}
          //         })
          //       }
          //     })
          //   },
          // })
        } else {
          return resolve({ code: -1 })
        }
      },
      failCb: (err) => {
        if (err.data.is_remind == 1) {
          wx.showModal({
            title: '开通通道失败',
            content: err.data.remind_str,
            cancelText: "修改卡信息",
            confirmText: "我知道了",
            success: (res) => {
              if (res.confirm) {
                console.log("我知道了")
              } else {
                console.log("修改卡信息")
                wx.navigateTo({
                  url: "/pages/creditCardReset/index",
                  success: function (res) {
                    res.eventChannel.emit('acceptDataFromOpenerPage', { cardInfo: cardInfo })
                  },
                })
              }
            }
          })
        }
      }
    })
  })
}
// function authChannelStep2SmsCode(orderNum, resp, callback) {
//   // TODO

//   http({
//     url:"/api/index/index?method=confirm_channel_authentication",
//     data: {
//       order_num:orderNum,
//       sms_code: ""
//     }
//   })

// }


module.exports = {
  formatTime: formatTime,
  authChannel: authChannel
}